import request from "@/utils/request";
import type { ResponseData, PageData } from "@/api/type";
import type { LoginForm, User, UserInfo, RolesResult } from "./type";

enum API {
    LOGIN_URL = "/admin/acl/index/login",
    USERINFO_URL = "/admin/acl/index/info",
    USER_PAGE_URL = "/admin/acl/user/",
    USER_UPDATE_URL = "/admin/acl/user/update",
    USER_SAVE_URL = "/admin/acl/user/save",
    USER_DELETE_URL = "/admin/acl/user/remove/",
    USER_BATCH_DELETE_URL = "/admin/acl/user/batchRemove",
    USER_INFO_URL = "/admin/acl/user/get/",
    USER_ROLE_ASSIGN_URL = "/admin/acl/user/toAssign/",
    Do_ASSIGN_ROLE_URL = "/admin/acl/user/doAssignRole"
}

const reqLogin = (data: LoginForm) => request.post<string, ResponseData<string>>(API.LOGIN_URL, data);
const reqUserInfo = () => request.get<string, ResponseData<User>>(API.USERINFO_URL);
const reqUserPage = (page: number, limit: number, username?: string) => request.get<string, ResponseData<PageData<UserInfo>>>(API.USER_PAGE_URL + `${page}/${limit}` + (username ? `?username=${username}` : ""));
const reqUserUpdate = (data: UserInfo) => request.put<string, ResponseData<string>>(API.USER_UPDATE_URL, data);
const reqUserSave = (data: UserInfo) => request.post<string, ResponseData<string>>(API.USER_SAVE_URL, data);
const reqUserDelete = (id: number) => request.delete<string, ResponseData<string>>(API.USER_DELETE_URL + `${id}`);
const reqUserBatchDelete = (ids: number[]) => request.delete<any, ResponseData<string>>(API.USER_BATCH_DELETE_URL, { data: ids });
const reqUserInfoById = (id: number) => request.get<string, ResponseData<UserInfo>>(API.USER_INFO_URL + `${id}`);
const reqUserRoleAssign = (id: number) => request.get<string, ResponseData<RolesResult>>(API.USER_ROLE_ASSIGN_URL + `${id}`);
const reqDoAssignRole = (data: { userId: number, roleIdList: number[] }) => request.post<string, ResponseData<string>>(API.Do_ASSIGN_ROLE_URL, data);

export { reqLogin, reqUserInfo, reqUserPage, reqUserUpdate, reqUserSave, reqUserDelete, reqUserBatchDelete, reqUserInfoById, reqUserRoleAssign, reqDoAssignRole }